const DEBUG = true;

function $( id, doc )
{
	if( typeof doc != 'undefined' )
	{
		return doc.getElementById( id );
	}
	return document.getElementById( id );
}

function create_element( name, params )
{ 
	var elem = document.createElement( name ); 

	for(  var key in params )
	{
		if( key == "cssClass" )
		{
			elem.className = params[key];
		}
	    else 
	    {
	       elem.setAttribute( key, params[key]);
	    }
	}
   //may the rest of the arguments be children
   for( var i = 2; i < arguments.length; i++ )
   {
		elem.appendChild( arguments[i] );
   }
   return elem; 
}

function removeChildren( node )
{
	while( node.firstChild ) 
	{
		node.removeChild( node.firstChild );
	}
}

function unique_id( date )
{
	if ( date == null )
	{ 
		date = new Date( );
	}
	return date.getTime( ) + Math.round( 900000000 + ( Math.random( ) * 100000000 ) );
}

function nl2newline( str )
{
	return str.replace( /[\n\r]/g, '_NL_' ); 
}

function newline2nl( str )
{
	if( typeof str == 'undefined' )
	{
		return '';
	}
	return str.replace( /_NL_/g, "\n" ); 
}

function in_array( needle, haystack )
{
	for( var i in haystack )
	{
		if( haystack[i] == needle )
		{
			return true;
		}
	}
	return false;
}

function trim( s )
{
	if ( s != null && s.length > 0 )
	{ 
		// Remove leading spaces and carriage returns
		while ((s.substring(0,1) == ' ') )
		{
		 	s = s.substring(1,s.length); 
		 }
		
		// Remove trailing spaces and carriage returns
		while( ( s.substring(s.length-1,s.length) == ' ') )
		{ 
			s = s.substring(0,s.length-1); 
		}
	}

	return s;
}

function addZero( num )
{
	return ( ( num > 9 ) ? "" : "0" ) + num;
}

function removeItem( arr, itemToRemove )
{
	var j = 0;
	while( j < arr.length )
	{
		if( arr[j] == itemToRemove )
		{
			arr.splice( j, 1 );
		} else { j++; }
	}
	return arr;
}